using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI.WebControls;
using FiltronASP.Models;
using FiltronASP.Models.DAO;
using FiltronASP.Models.DAO.Interfaces;
using FiltronASP.Modules.Abstracts;

namespace FiltronASP.Helpers.HtmlTags
{
    public static class ContentPlaceHolderHelper
    {
        /// <summary>
        /// Metoda odpowiedzialna za generowanie contentu zmiennej dla place holdera w szablonie.
        /// </summary>
        /// <param name="contentPlaceHolder">place holder z c#</param>
        /// <param name="page">biezaca strona</param>
        /// <returns></returns>
        public static string GenerateCmsContent(this ContentPlaceHolder contentPlaceHolder, Page page)
        {
            try
            {
                if (page == null)
                {
                    return String.Empty;
                }   //if
                if (page.template_id == null)
                {
                    return String.Empty;
                }   //if
                IVariableValueDAO variableValueDAO = new VariableValueDAO();
                VariableValue variableValue = variableValueDAO.FindByVariableAndPage(contentPlaceHolder.ID, page.page_id, Convert.ToInt32(page.language_id));
                if (variableValue != null)
                {
                    return variableValue.variable_value;
                }   //if
                Object moduleClass = Activator.CreateInstance(Type.GetType(String.Format("FiltronASP.Modules.{0}", contentPlaceHolder.ID)));
                if (moduleClass != null)
                {
                    if (moduleClass is AbstractModule)
                    {
                        return (moduleClass as AbstractModule).GenerateContent(page);
                    }   //if
                }   //if
            }   //try
            catch (Exception ex)
            {
                Logger.MyLogger.Log(ex);
            }   //catch
            return String.Empty;
        }

        /// <summary>
        /// Metoda odpowiedzialna za generowanie tytulu dla szablonu.
        /// </summary>
        /// <param name="contentPlaceHolder">place holder z c#</param>
        /// <param name="page">biezaca strona</param>
        /// <returns></returns>
        public static string GenerateTitleContent(this ContentPlaceHolder contentPlaceHolder, Page page)
        {
            try
            {
                if (page == null)
                {
                    return String.Empty;
                }   //if
                return page.page_name;
            }   //try
            catch (Exception ex)
            {
                Logger.MyLogger.Log(ex);
            }   //catch
            return String.Empty;
        }

        /// <summary>
        /// Metoda odpowiedzialna za generowanie metadanych dla szablonu.
        /// </summary>
        /// <param name="contentPlaceHolder">place holder z c#</param>
        /// <param name="page">biezaca strona</param>
        /// <returns></returns>
        public static string GenerateKeywordsContent(this ContentPlaceHolder contentPlaceHolder, Page page)
        {
            try
            {
                if (page == null)
                {
                    return String.Empty;
                }   //if
                return "<meta name=\"keywords\" content=\"" + page.page_keywords + "\" />";
            }   //try
            catch (Exception ex)
            {
                Logger.MyLogger.Log(ex);
            }   //catch
            return String.Empty;
        }

        /// <summary>
        /// Metoda odpowiedzialna za generowanie slow kluczowych dla szablonu.
        /// </summary>
        /// <param name="contentPlaceHolder">place holder z c#</param>
        /// <param name="page">biezaca strona</param>
        /// <returns></returns>
        public static string GenerateMetaContent(this ContentPlaceHolder contentPlaceHolder, Page page)
        {
            try
            {
                if (page == null)
                {
                    return String.Empty;
                }   //if
                return "<meta name=\"description\" content=\"" + page.page_meta + "\" />";
            }   //try
            catch (Exception ex)
            {
                Logger.MyLogger.Log(ex);
            }   //catch
            return String.Empty;
        }

    }
}
